package com.ai5suoai.ams.repository;

import com.ai5suoai.ams.entity.User;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.Optional;

/**
 * 用户的数据访问
 *
 * @author chenzhenjia
 * @since 2016/3/17
 */
public interface UserRepository extends MongoRepository<User, String> {
    /**
     * 根据用户名查询用户对象
     *
     * @param username 用户名
     * @return 返回用户的 Optional
     */
    @Cacheable(value = "user", keyGenerator = "cacheGenerator", unless = "#result == null")
    User findOneByUsername(String username);

    /**
     * 根据邮箱查询用户对象
     *
     * @param email 邮箱
     * @return 返回用户的 optional 对象
     */
    @Cacheable(value = "user", keyGenerator = "cacheGenerator", unless = "#result == null")
    User findOneByEmail(String email);

    /**
     * 根据激活的key 查找用户
     *
     * @param key 激活key
     * @return 用户的optional 对象
     */
    Optional<User> findOneByActivationKey(String key);

}
